package com.pri.labs.junk;

import java.util.Collection;
import java.util.List;

import com.pri.labs.client.structure.Action;
import com.pri.labs.client.structure.Dock;
import com.pri.labs.client.structure.Dock.Type;
import com.pri.labs.client.ui.control.AbstractEditor;
import com.pri.labs.client.ui.control.Editor;
import com.pri.labs.client.ui.control.LayoutEditor;
import com.pri.labs.client.ui.module.DockPanel;
import com.pri.labs.client.ui.module.EditorViewPort;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.util.ValueCallback;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;

public class DockEditorOld extends AbstractEditor implements Editor
{
 public enum Actions
 {
  SPLIT2COL,
  SPLIT2ROW,
  SET_W,
  SET_H,
  REMOVE
 }
 

 private LayoutEditor layEdit;
 private DockEditorOld parent;
 private Dock dock;
 private Canvas panel;
 private Action actins;

 public DockEditorOld(LayoutEditor ledit, Dock dk, DockEditorOld parent)
 {
  super();
  dock=dk;
  layEdit=ledit;
  this.parent=parent;
  
  actins = new Action(null,null,null, new Action[]{
    new Action("Split to columns",Actions.SPLIT2COL.name(),"/images/silk/accept.png",null),
    new Action("Split to rows",Actions.SPLIT2ROW.name(),"/images/silk/accept.png",null),
    new Action("Set width",Actions.SET_W.name(),"/images/silk/accept.png",null),
    new Action("Set height",Actions.SET_H.name(),"/images/silk/accept.png",null),
    new Action("Remove",Actions.REMOVE.name(),"/images/silk/accept.png",null),
    
  });
 }

 @Override
 public void activate( EditorViewPort cnv )
 {
  layEdit.activate(cnv);
 }

 @Override
 public void deactivate()
 {
  // TODO Auto-generated method stub
  
 }

 @Override
 public Action getAction()
 {
  return actins;
 }

 @Override
 public String getIcon()
 {
  return "/images/silk/accept.png";
 }

 @Override
 public String getName()
 {
  return dock.getName();
 }

 public Dock getDock()
 {
  return dock;
 }
 
 @Override
 public List<Editor> getSubEditors()
 {
  // TODO Auto-generated method stub
  return null;
 }

 @Override
 public void actionPerformed(String action, Editor object)
 {
  if( Actions.SPLIT2ROW.name().equals(action) )
  {
   SC.askforValue("Enter number of rows", new ValueCallback()
   {

    @Override
    public void execute(String value)
    {
     int rowNum = 0;

     try
     {
      rowNum = Integer.parseInt(value);
     }
     catch(Exception e)
     {
      return;
     }

     if(rowNum <= 0 || rowNum > 10)
      return;

     if(parent.getDock().getType() == Type.VSPLIT)
     {
      VLayout vl = (VLayout) panel;

      int ind = 0;
      for(Canvas cn : vl.getMembers())
      {

       if(cn == DockPanel.this)
       {
        int maxNm = findMaxDockNameSuffix(dock);
        
        for(int k=1; k < rowNum; k++ )
        {
         Dock dk = new Dock();
         dk.setParent(dock);
         dk.setName(defaultDockNamePrefix+(++maxNm));
         dk.setWidth(-100);
         dk.setHeight(0);
         
         vl.addMember(new DockPanel("100%","*",dk,vl), ind + 1);
        }
        
        break;
       }

       ind++;
      }
     }
     else
     {
      HLayout hl = (HLayout) DockPanel.this.getParentElement();

      int ind = 0;
      for(Canvas cn : hl.getMembers())
      {

       if(cn == DockPanel.this)
       {
        cn.destroy();

        VLayout vl = new VLayout(2);
        vl.setWidth100();
        vl.setHeight100();

        for(int k=0; k < rowNum; k++ )
         vl.addMember(new DockPanel("100%","*",new Dock(),vl));

        hl.addMember(vl, ind);

        break;
       }

       ind++;

      }
     }
    }
   });

  }
  
  
  MenuItem colsItem = new MenuItem();
  colsItem.setTitle("Split to columns");
  colsItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler()
  {

   @Override
   public void onClick(MenuItemClickEvent event)
   {
    SC.askforValue("Enter number of columns", new ValueCallback()
    {
     
     @Override
     public void execute(String value)
     {
      int colNum = 0;
      
      try
      {
       colNum = Integer.parseInt(value);
      }
      catch (Exception e)
      {
       return;
      }
      
      if( colNum <= 0 || colNum > 10 )
       return;
      
      if( ! (DockPanel.this.getParentElement() instanceof VLayout ) )
      {
       HLayout hl = (HLayout) DockPanel.this.getParentElement();
       
       int ind=0;
       for(Canvas cn : hl.getMembers() )
       {
        
        if( cn == DockPanel.this )
        {
         
         for(int k=1; k < colNum; k++ )
          hl.addMember( new DockPanel("*","100%",new Dock(),hl), ind+1 );
         
         break;
        }
        
        ind++;
       }
      }
      else
      {
       VLayout vl = (VLayout) DockPanel.this.getParentElement();
       
       int ind=0;
       for(Canvas cn : vl.getMembers() )
       {
        
        if( cn == DockPanel.this )
        {
         cn.destroy();
         
         HLayout hl = new HLayout(2);
         hl.setWidth100();
         hl.setHeight100();

         
         for(int k=0; k < colNum; k++ )
          hl.addMember( new DockPanel("*","100%",new Dock(),hl) );
         
         vl.addMember(hl,ind);

         break;
        }
        
        ind++;

       }
      }
     }
    });
    

   }
  });

  MenuItem widthItem = new MenuItem();
  widthItem.setTitle("Set width");
  widthItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler()
  {

   @Override
   public void onClick(MenuItemClickEvent event)
   {
    SC.askforValue("Enter dock width", new ValueCallback()
    {

     @Override
     public void execute(String value)
     {
      if( value == null )
       return;

      try
      {
       convertSizeToNumber(value);
      }
      catch (Exception e)
      {
       SC.warn("Invalid value", MessageCB.getInstance());
       return;
      }

      setWidth(value);
      setContents("Width: "+widthStr+"<br>Height: "+heightStr);
     }
     
    });
    
   }

  }
  );
  
  
  MenuItem heightItem = new MenuItem();
  heightItem.setTitle("Set height");
  heightItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler()
  {

   @Override
   public void onClick(MenuItemClickEvent event)
   {
    SC.askforValue("Enter dock height", new ValueCallback()
    {

     @Override
     public void execute(String value)
     {
      if( value == null )
       return;
      
      try
      {
       convertSizeToNumber(value);
      }
      catch (Exception e)
      {
       SC.warn("Invalid value", MessageCB.getInstance() );
       return;
      }
      
      setHeight(value);
      setContents("Width: "+widthStr+"<br>Height: "+heightStr);
     }
     
    });
    
   }

  }
  );
  
  MenuItem removeItem = new MenuItem();
  removeItem.setTitle("Remove");
  removeItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler()
  {

   @Override
   public void onClick(MenuItemClickEvent event)
   {
    SC.confirm("Do you really want to remove dock?", new BooleanCallback()
    {
     
     @Override
     public void execute(Boolean value)
     {
      if( value != null && value.booleanValue() )
       DockPanel.this.destroy();
     }
    });
    
   }

  }
  );
 }


 
// @Override
// public void actionPerformed(String action, Editor object)
// {
//  // TODO Auto-generated method stub
//  
// }

 @Override
 public void actionPerformed(String action, Collection<Editor> objects)
 {
  // TODO Auto-generated method stub
  
 }

 public Canvas getPanel()
 {
  return panel;
 }

 public void setPanel(Canvas panel)
 {
  switch( dock.getType() )
  {
   case HSPLIT:
    panel.setBorder("1px dashed green");
    break;

   case VSPLIT:
    panel.setBorder("1px dashed blue");
    break;
    
   case DOCK:
    panel.setBorder("1px dotted black");
    break;
  }
  
  this.panel = panel;
 }

}
